package com.roc.web.action.announce;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.newPage.IPageService;
import com.roc.page.BaseAction;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Announcement;
import com.roc.pojo.Operators;
import com.roc.service.IAnnounceService;
import com.roc.service.IPagenitionService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;
import com.roc.web.action.estrn.BigEstrnHisAction;
import com.roc.web.action.operator.OperatorCheckSearchCondition;
@Component
@Scope("prototype")
@Namespace("/announce")
public class AccnounceAction  extends PaginationBaseAction {
	 private Logger logger=LogManager.getLogger(AccnounceAction.class);
	 private AnnounceSearchCondition searchCondition;
	 private Announcement announce;
	 
	 @Autowired
	 IPagenitionService pagenitionService;
	 @Autowired 
	 IAnnounceService announceService;
	 
	 @Autowired
	 private IPageService pageService;

	 
	
	 
	 @Action(value = "init", results = { @Result(name = "success", location = "/pages/announce/queryAnnounce.jsp")
	 ,@Result(name = "error", location = "/error.jsp")
	    })
		public String init(){
		 
		 Operators op = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			if(op.getRoleType().equals(Role.AGENT.getCode())&&!"sdsyb".equals(op.getAccountNo())) {
				addActionError("当前登录账号无权限访问!!!");
				return ERROR;
			}
			return SUCCESS;
		}
	 
	 @Action(value = "queryAnnouncement", results = { @Result(name = "success", location = "/pages/announce/queryAnnounce.jsp")
	 ,@Result(name = "error", location = "/error.jsp")
	    })
	 public String queryAnnouncement(){
		 try{
			 Operators op = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
				if(op.getRoleType().equals(Role.AGENT.getCode())&&!"sdsyb".equals(op.getAccountNo())) {
					addActionError("当前登录账号无权限访问!!!");
					return ERROR;
				}
			 if(searchCondition==null){
					searchCondition=(AnnounceSearchCondition)getSearchConditionFromSession();
					if(searchCondition==null){
						searchCondition=new AnnounceSearchCondition();
					}
				}
			 
			 int rows=pageService.count(searchCondition);
			 if(rows>0){
				 searchRsList=pageService.findAll(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
					
			 }
			 
			 this.setTotalPage(this.calcuTotalPage(rows));
			 getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			 getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			 getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			 setSearchToConditionSession(searchCondition);
				
		 }catch(ServiceException e){
			 logger.error(e.getMessage(),e);
			 addActionError(e.getMessage());
		 }
		 
		 return SUCCESS;
	 }
	 
	 @Action(value = "viewAnnounce", results = { @Result(name = SUCCESS, location = "/pages/announce/viewAnnounce.jsp")
	    })
	 public String viewAnnounce(){
		 try{
			 announce=announceService.queryAnnouncementById(announce.getId());
			 
		 }catch(ServiceException e){
			 logger.error(e.getMessage(),e);
			 addActionError(e.getMessage());
		 }
		 return SUCCESS;
	 }
	 
	 @Action(value = "addAnnounce", 
			 results = { @Result(name = SUCCESS, location = "/pages/announce/addInformation.jsp")
	    })
	 public String addAnnounce(){
		 
		 return SUCCESS;
	 }
	 @Action(value = "saveAdd", results = { @Result(name = SUCCESS, location = "/pages/announce/queryAnnounce.jsp")
	    })
	 public String saveAdd(){
		 Operators op = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		 try{
			 announce.setOperators(op.getAccountNo());
			 announceService.addAnnouncement(announce);
		 }catch(ServiceException e){
			 logger.error(e.getMessage(),e);
			 addActionError(e.getMessage());
		 }
		 
		 return queryAnnouncement();
	 }
	 
	 @Action(value = "editAnnounce", results = { @Result(name = SUCCESS, location = "/pages/announce/ediAnnounce.jsp")
	    })
	 public String editAnnounce(){
		try{
			announce=announceService.queryAnnouncementById(announce.getId());
		}catch(ServiceException e){
			 logger.error(e.getMessage(),e);
			 addActionError(e.getMessage());
		 }
		 return SUCCESS;
	 }
	 
	 @Action(value = "saveEdit", results = { @Result(name = SUCCESS, location = "/pages/announce/queryAnnounce.jsp")
	    })
	 public String saveEdit(){
		 Operators op = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		 try{
			 announce.setOperators(op.getAccountNo());
			 announceService.editSaveAnnouncement(announce);
		 }catch(ServiceException e){
			 logger.error(e.getMessage(),e);
			 addActionError(e.getMessage());
		 }
		 
		 return queryAnnouncement();
	 }
	 /**
	  *删除公告
	  * */
	 @Action(value = "deleteAnn", results = { @Result(name = SUCCESS, location = "/pages/announce/queryAnnounce.jsp")
	    })
	 public String deleteAnn(){
		 try{
			 announceService.deleteAnnouncementById(announce.getId());
		 }catch(ServiceException e){
			 logger.error(e.getMessage(),e);
			 addActionError(e.getMessage());
		 }
		 
		 return queryAnnouncement();
	 }
	 /**
	  * 登陆成功，页面展现公告
	  * */
	 @Action(value = "announcementInit", 
			 results = { @Result(name = SUCCESS, location = "/pages/announce/initAnnounce.jsp")
	    })
	 public String queryAnnouncementInit(){
		 try{
			 
			 if(searchCondition==null){
					searchCondition=(AnnounceSearchCondition)getSearchConditionFromSession();
					if(searchCondition==null){
						searchCondition=new AnnounceSearchCondition();
					}
				}
			 int rows=pageService.count(searchCondition);
			 if(rows>0){
				 searchRsList=pageService.findAll(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
					
			 }
			 this.setTotalPage(this.calcuTotalPage(rows));
			 getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			 getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			 getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			 setSearchToConditionSession(searchCondition);
		 }catch(ServiceException e){
			 logger.error(e.getMessage(),e);
			 addActionError(e.getMessage());
		 }
		 
		 return SUCCESS;
	 }
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		// TODO Auto-generated method stub
		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		// TODO Auto-generated method stub
		getSession().setAttribute(SEARCH_SESSION_ANNOUNCE_QUERY, searchCondition);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (BaseSearchCondition) getSession().getAttribute(SEARCH_SESSION_ANNOUNCE_QUERY);
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE+"";
	}

	public AnnounceSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(AnnounceSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

	public Announcement getAnnounce() {
		return announce;
	}

	public void setAnnounce(Announcement announce) {
		this.announce = announce;
	}
	
	
}
